package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.IntField;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.mjdb.data.field.Point2DField;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public  class DataPoint extends Point2DField implements IPrimative{
	public static String DEFAULT_ID="_Point";

	public TagModifier _modifier = TagModifier.POINT_TYPE_POINT;
public DataPoint() {
	try{
		setId(_modifier.DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public DataPoint(int x, int y) {

	super(x, IntField.INT_MAX_NEG, IntField.INT_MAX_POS, y, IntField.INT_MAX_NEG, IntField.INT_MAX_POS);

	try {
		setId(_modifier.DEFAULT_ID, this);
	} catch (LockException le) {
	} //Dummy catch
}
public DataPoint(int x, int xMin, int xMax, int y, int yMin, int yMax) {

	super(x, xMin, xMax, y, yMin, yMax);

	try {
		setId(_modifier.DEFAULT_ID, this);
	} catch (LockException le) {
	} //Dummy catch
}
public DataPoint(TagModifier type) {
	_modifier = type;

	try{
		setId(_modifier.DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public DataPoint(TagModifier type, int x, int y) {

	super(x, y);

	_modifier = type;

	try {
		setId(_modifier.DEFAULT_ID, this);
	} catch (LockException le) {
	} //Dummy catch
}
public DataPoint(TagModifier type, int x, int xMin, int xMax, int y, int yMin, int yMax) {

	super(x, xMin, xMax, y, yMin, yMax);

	_modifier = type;

	try {
		setId(_modifier.DEFAULT_ID, this);
	} catch (LockException le) {
	} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{

	String attribName;
	String attribValue;

	for(int loop=0;loop<xml.getAttributeCount();loop++)
	{
		attribName = xml.getAttributeNameByIndex(loop);
		attribValue = xml.getAttributeValueByIndex(loop);

		if(attribName.equals(Token.ID))
		{
			if(attribValue!=null)
			{
				try{
					setId(attribValue,this);
				}
				catch(LockException le){} //Dummy catch
			}
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.X)))
		{
			try{
				setValue(Token.X,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.X_MIN)))
		{
			try{
				setValue(Token.X_MIN,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
  		else if(attribName.equals(_modifier.getAttributeAlias(Token.X_MAX)))
		{
			try{
				setValue(Token.X_MAX,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.Y)))
		{
			try{
				setValue(Token.Y,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
  		else if(attribName.equals(_modifier.getAttributeAlias(Token.Y_MIN)))
		{
			try{
				setValue(Token.Y_MIN,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.Y_MAX)))
		{
			try{
				setValue(Token.Y_MAX,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if( !NameHelper.isSystemName(attribName) )
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_ATTRIB_CODE ,new Object[]{attribName,Token.POINT,""+xml.getStartLineNo()} );
		}
	}

}
public void buildFromgrfxML(XmlElement xml,TagModifier type)
{
	_modifier = type;
	buildFromgrfxML(xml);
}
public void copyFromgrfxML(XmlElement xml)
{

	String attribName;
	String attribValue;

	for(int loop=0;loop<xml.getAttributeCount();loop++)
	{
		attribName = xml.getAttributeNameByIndex(loop);
		attribValue = xml.getAttributeValueByIndex(loop);

		if(attribName.equals(Token.ID))
		{
			// ignore
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.X)))
		{
			try{
				setValue(Token.X,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.X_MIN)))
		{
			try{
				setValue(Token.X_MIN,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
  		else if(attribName.equals(_modifier.getAttributeAlias(Token.X_MAX)))
		{
			try{
				setValue(Token.X_MAX,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.Y)))
		{
			try{
				setValue(Token.Y,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
  		else if(attribName.equals(_modifier.getAttributeAlias(Token.Y_MIN)))
		{
			try{
				setValue(Token.Y_MIN,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.Y_MAX)))
		{
			try{
				setValue(Token.Y_MAX,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_ATTRIB_CODE ,new Object[]{attribName,Token.SIZE,""+xml.getStartLineNo()} );
		}
	}

}
public void copyFromgrfxML(XmlElement xml,TagModifier type)
{
	_modifier = type;
	copyFromgrfxML(xml);
}
public DataPoint getClone() {
	return this.getClone(_modifier);
}
public DataPoint getClone(String path) {
	return this.getClone(_modifier,path);
}
public DataPoint getClone(TagModifier modifier) {

	DataPoint newPoint = new DataPoint(modifier);

	try{
		newPoint.setValueX(getValueX(),this);
		newPoint.setValueY(getValueY(),this);
	}
	catch(LockException le){}//dummy catch

	newPoint.setValueXMin(getValueXMin());
	newPoint.setValueYMin(getValueYMin());

	newPoint.setValueXMax(getValueXMax());
	newPoint.setValueYMax(getValueYMax());

	return newPoint;
}
public DataPoint getClone(TagModifier modifier,String path) {

	DataPoint newPoint = getClone(modifier);

	GrfxMLStoreProcessor.setCloneName( path,this,newPoint );

	return newPoint;
}
public TagModifier getModifier() {
	return _modifier;
}
public void register(DataLinker store) {
	super.register(store);
}
public void replace(Field oldObj, Field newObj) {
}
public void replace(Field oldObj, Field newObj,boolean recursive) {
}
public void resetChanged() {
	super.resetChanged();
}
public String togrfxML(String name) {
	return IPrimativeHelper.togrfxML(this,name);
}
public String togrfxML(String name,TagModifier modifier) {
	return IPrimativeHelper.togrfxML(this,name,modifier);
}
public String togrfxML(IPrimative defaultField,String name) {
	return IPrimativeHelper.togrfxML(defaultField,this,name,null);
}
public String togrfxML(IPrimative defaultField,String name,TagModifier modifier) {
	return IPrimativeHelper.togrfxML(defaultField,this,name,modifier);
}

public String toString()
{
	return IPrimativeHelper.toString(this);
}
protected void update()
{
	try{
		setValueX(getValueXMax()-getValueXMin(),this);
		setValueY(getValueYMax()-getValueYMin(),this);
	} catch(LockException le){}//dummy catch
}
}
